<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PayNotifyApi } from '#/api/pay/notify';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { ElDivider } from 'element-plus';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getNotifyTaskDetail } from '#/api/pay/notify';
import { useDescription } from '#/components/description';

import { useDetailLogColumns, useDetailSchema } from '../data';

const formData = ref<PayNotifyApi.NotifyTask>();

const [Description] = useDescription({
  componentProps: {
    border: true,
    column: 2,
    direction: 'horizontal',
    labelWidth: 140,
    title: '',
    extra: '',
  },
  schema: useDetailSchema(),
});

const [LogGrid, logGridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useDetailLogColumns(),
    height: 'auto',
    keepSource: true,
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    pagerConfig: {
      enabled: false,
    },
    toolbarConfig: {
      enabled: true,
      refresh: true,
    },
  } as VxeTableGridOptions,
});

const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    const data = modalApi.getData<PayNotifyApi.NotifyTask>();
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getNotifyTaskDetail(data.id);
      logGridApi.grid?.reloadData(formData.value?.logs || []);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>

<template>
  <Modal
    title="通知详情"
    class="w-1/2"
    :show-cancel-button="false"
    :show-confirm-button="false"
  >
    <Description :data="formData" />

    <ElDivider />

    <LogGrid table-title="支付通知列表" />
  </Modal>
</template>
